/**
 * Arquivo Original : Chat.java
 * Criado por: carlos_dias
 * Criação : 20/10/2012-19:56:03
 *
 */
package ch02.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * @author carlos_dias
 *
 */
public class Chat implements MessageListener{
	private TopicSession pubSession;
	private TopicPublisher publisher;
	private TopicConnection connection;
	private String username;
	
	

	/**
	 * @throws NamingException 
	 * 
	 */
	public Chat(String topicFactory, String topicName, String username) throws JMSException, NamingException {
		Properties env = new Properties();//chat
		env.put(Context.SECURITY_PRINCIPAL, "system");
	
		env.put(Context.SECURITY_CREDENTIALS, "manager");
		env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory" );
		env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		
		//InitialContext ctx = new InitialContext(env);
		InitialContext ctx = new InitialContext();
		
		TopicConnectionFactory  conFactory = (TopicConnectionFactory)ctx.lookup(topicFactory);
		
		TopicConnection connection = conFactory.createTopicConnection();
		
		TopicSession pubSession = connection.createTopicSession(false,  Session.AUTO_ACKNOWLEDGE);
		
		TopicSession subSession = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
		
		Topic chatTopic = (Topic)ctx.lookup(topicName);
		
		TopicPublisher publisher = pubSession.createPublisher(chatTopic);
		
		TopicSubscriber subscriber = subSession.createSubscriber(chatTopic, null, true);
		
		subscriber.setMessageListener(this);
		
		this.connection = connection;
		
		this.pubSession = pubSession;
		this.publisher = publisher;
		this.username = username;
		
		connection.start();
		
	}



	/* (non-Javadoc)
	 * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
	 */
	public void onMessage(Message message) {
		try {
			TextMessage mensagem = (TextMessage) message;
			System.out.println(mensagem.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	protected void writeMessage(String text) throws JMSException{
		TextMessage mensagem = pubSession.createTextMessage();
		mensagem.setText(username + " -> " + text);
		
		publisher.publish(mensagem);
	}
	
	
	public void close() throws JMSException{
		connection.close();
	}
	
	
	public static void main(String[] args) {
		try {
			if (args.length != 3){
				System.out.println("Fabrica, topico, ou nome estão faltando");
			}
			Chat chat = new Chat(args[0], args[1], args[2]);
			
			BufferedReader comando = new BufferedReader(new InputStreamReader(System.in));
			
			while(true){
				String s = comando.readLine();
				
				if (s.equalsIgnoreCase("exit")){
					chat.close();
					System.exit(0);
				}
				else{
					chat.writeMessage(s);
				}
				
			}
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
